package com.atgugui.eduservice.Listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atgugui.eduservice.entity.EduSubject;
import com.atgugui.eduservice.entity.ExcelSubjectData;
import com.atgugui.eduservice.service.EduSubjectService;
import com.atgugui.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;

/**
 * 通过监听器读取Excel中的数据，按行读取，然后将读取到的行数据存放到数据库中
 * 注意：我们存放数据库的操作实际上必须通过EduSubjectService对象实现，但是
 * 该监听器不是由Spring来管理，所以我们无法通过注入的方式将service对象注入
 * 进来，但是我们可以通过有参构造方法实现
 */
@SuppressWarnings("all")
public class SubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {

    public EduSubjectService eduSubjectService;

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    public SubjectExcelListener() {

    }

    /**
     * 一级分类的parentId = 0
     * 二级分类的parentId 采用uuid生成唯一id
     * @param excelSubjectData
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        if (excelSubjectData == null) {
            throw new GuLiException(20001,"数据为空");
        }
        String oneSubjectName = excelSubjectData.getOneSubjectName(); // 一级
        String twoSubjectName = excelSubjectData.getTwoSubjectName(); // 二级
        String pid = "";
        EduSubject eduOneSubject = this.existOneSubject(eduSubjectService, oneSubjectName);
        if (eduOneSubject == null) {
            eduOneSubject = new EduSubject();
            eduOneSubject.setTitle(oneSubjectName);
            eduOneSubject.setParentId("0");
            eduSubjectService.save(eduOneSubject);
        }
        pid = eduOneSubject.getId();
        EduSubject eduTwoSubject = this.existTwoSubject(eduSubjectService, twoSubjectName, pid);
        if (eduTwoSubject == null) {
            eduTwoSubject = new EduSubject();
            eduTwoSubject.setTitle(twoSubjectName);
            eduTwoSubject.setParentId(pid);
            eduSubjectService.save(eduTwoSubject);
        }
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    // 判断一级分类是否重复
    public EduSubject existOneSubject(EduSubjectService eduSubjectService, String name) {
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",name);
        queryWrapper.eq("parent_id","0"); // 判断当前传入的行数据的parent_id是否为0
        EduSubject eduSubject = eduSubjectService.getOne(queryWrapper);
        return eduSubject;
    }

    // 判断二级分类是否重复
    public EduSubject existTwoSubject(EduSubjectService eduSubjectService, String name, String pid) {
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",name);
        queryWrapper.eq("parent_id",pid); // 判断当前传入的行数据的parent_id是否为0
        EduSubject eduSubject = eduSubjectService.getOne(queryWrapper);
        return eduSubject;
    }
}
